package com.AUI.components
{
	import com.AUI.*;
	import com.AUI.events.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.utils.*;
	/**
	 * Container,is used to contain any display object,and provide scrolling feature.
	 * <p>It owns 3 helper components: 2 scrollbars and 1 corner button.</p>
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class Container extends ScrollBase
	{
		/**
		 * Constructor.
		 */
		public function Container()
		{
			// init attributes
			depth = 3;
			
			/**************** INITCOMPLETE ****************/
			Object(this).constructor == Container && initComplete();
		}
		/**
		 * The number of visual elements in this component.
		 * @see #addElement()
		 * @see #addElementAt()
		 * @see #removeElement()
		 * @see #removeElementAt()
		 * @see #removeElements()
		 * @see #replaceElementAt()
		 */
		public function get numElements():int
		{
			return elements.numChildren;
		}
		/**
		 * Retrieves the element at the specified index.
		 * @param index The index of the element to be retrieved.
		 * @return The object at the specified index location.
		 */
		public function getElementAt(index:int):DisplayObject
		{
			return elements.getChildAt(index);
		}
		/**
		 * Returns the index position of the element.
		 * @param element The DisplayObject instance to identify.
		 * @return The index position of the child display object to identify.
		 */
		public function getElementIndex(element:DisplayObject):int
		{
			return elements.getChildIndex(element);
		}
		/**
		 * Appends an element to the end of the list of elements.
		 * @param element The element to be added to the list.
		 * @return The element that was added.
		 * @see #itemsLength
		 */
		public function addElement(element:DisplayObject):DisplayObject
		{
			return addElementAt(element,numElements);
		}
		/**
		 * Inserts an element into the list at the specified index location.
		 * @param element The element to be added to the list.
		 * @param index The index at which to add the element.
		 * @return The element that was added.
		 * @see #itemsLength
		 */
		public function addElementAt(element:DisplayObject,index:int):DisplayObject
		{
			elements.addChildAt(element,index);
			updateContentBox();
			return element;
		}
		/**
		 * Removes an element from container.
		 * @param element The element to be removed.
		 * @return The element that was removed.
		 * @see #itemsLength
		 */
		public function removeElement(element:DisplayObject):DisplayObject
		{
			return removeElementAt(elements.getChildIndex(element));
		}
		/**
		 * Removes the element at the specified index position.
		 * @param index The index of the element to be removed.
		 * @return The element that was removed.
		 * @see #itemsLength
		 */
		public function removeElementAt(index:int):DisplayObject
		{
			var element:DisplayObject = elements.removeChildAt(index);
			updateContentBox();
			return element;
		}
		/**
		 * Removes all elements from the contianer.
		 * @see #itemsLength
		 */
		public function removeElements():void
		{
			var len:int = numElements;
			while(len--)
			{
				elements.removeChildAt(0);
			}
			elements.x = elements.y = 0;
			updateContentBox();
		}
		/**
		 * Replaces the element at the specified index location with another element.
		 * @param element The element to replace the element at the specified index location.
		 * @param index The index position of the element to be replaced.
		 * @return The element that was replaced.
		 */
		public function replaceElementAt(element:DisplayObject,index:int):DisplayObject
		{
			var removeElement:DisplayObject = elements.removeChildAt(index);
			elements.addChildAt(element,index);
			updateContentBox();
			return removeElement;
		}
		/**
		 * Set all elements' attribute to a same value.
		 * @param name Attribute name.
		 * @param value Attribute value.
		 */
		public function setElements(name:String,value:*):void
		{
			for(var i:int = 0,len:int = numElements;i < len;++i)
			{
				var element:DisplayObject = getElementAt(i);
				if(element[name] != value)
				{
					element[name] = value;
				}
			}
		}
		/**
		 * Get all elements' attribute.
		 * @param name Attribute name.
		 */
		public function getElements(name:String):Array
		{
			var values:Array = new Array;
			for(var i:int = 0,len:int = numElements;i < len;++i)
			{
				var element:DisplayObject = getElementAt(i);
				values.push(element[name]);
			}
			return values;
		}
	}
}